/*
 * @Author: Aro
 * @Date: 2021-10-07 21:17:21
 * @LastEditTime: 2021-10-07 21:58:38
 * @LastEditors: Please set LastEditors
 */

// 给定一个正整数 N，试求有多少组连续正整数满足所有数字之和为 N?
// 1 <= N <= 10 ^ 9
// 等差数列
/**
 * @param {number} n 
 * @return {number} 
 */
var consecutiveNumbersSum = function(n) {
  var result = 0;
  var k = 1;
  for (k; (n - k * (k - 1) / 2) > 0; k++) {
    if ((n - k * (k - 1) / 2) % k === 0) result++;
  }
  return result;
};

// (a1 + ak) * k / 2 = n
// (2a + k - 1) * k = 2n
// 2ka + k(k - 1) = 2n
// ka = n - k(k - 1) / 2 > 0
console.log("====== start ======");

let val1 = consecutiveNumbersSum(1);
let val5 = consecutiveNumbersSum(5);
let val9 = consecutiveNumbersSum(9);
let val15 = consecutiveNumbersSum(15);

console.log('输入1的结果:' + val1 + ',输入5的结果:' + val5 + ',输入9的结果:' + val9 + ',输入15的结果:' + val15);
console.log("====== end ======");